package com.neouton.crm.executiceManager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.executiceManager.entity.ManagerPurchaseOrderVO;
import com.neouton.crm.executiceManager.service.IManagerPurchaseOrderService;
import com.neouton.crm.managerPayPlan.entity.ManagerPayPlan;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("manager")
public class ManagerPurchaseOrderController {

    @Resource
    private IManagerPurchaseOrderService managerPurchaseOrderService;

    /**
     * 分页查新采购信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryPurchaseOrdersByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryPurchaseOrdesByPage(@PathVariable(value = "pageNum") int pageNum,
                                                @PathVariable(value = "pageSize") int pageSize,
                                                @RequestBody ManagerPurchaseOrderVO entity,
                                                @RequestHeader("token") String token) {
        IPage<ManagerPurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPurchaseOrderVO> result = managerPurchaseOrderService.queryPurchaseOrdesByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 保存采购信息
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/saveOrUpdatePurchaseOrde")
    public ResponseAPI savePurchasesoInfo(@RequestBody ManagerPurchaseOrderVO purchaseOrder, @RequestHeader("token") String token){
        purchaseOrder.setCreator(AuthUtils.getUserId());
        purchaseOrder.setLastUpdateBy(AuthUtils.getUserId());
        return ResponseAPI.successData(managerPurchaseOrderService.saveOrUpdatePurchaseOrder(purchaseOrder,token));
    }

    /**
     * 更具采购单号获取采购信息
     * @return
     */
    @PostMapping("/getPurchaseOrdeInfoById/{purchaseOrdeId}")
    public ResponseAPI getPurchaseOrdeInfoById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        ManagerPurchaseOrderVO purchaseOrde = managerPurchaseOrderService.getPurchaseOrdeInfoById(purchaseOrdeId);
        return ResponseAPI.successData(purchaseOrde);
    }

    /**
     * 采购信息驳回审批
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/rejectionPurchaseOrderInfo")
    public ResponseAPI rejectionPurchaseOrderInfo(@RequestBody ManagerPurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        return ResponseAPI.successData(managerPurchaseOrderService.rejectionPurchaseOrderInfo(purchaseOrder, token));
    }

    /**
     * 采购信息通过审批
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/approvePurchaseOrderInfo")
    public ResponseAPI approvePurchaseOrderInfo(@RequestBody ManagerPurchaseOrderVO purchaseOrder,@RequestHeader("token") String token){
        return ResponseAPI.successData(managerPurchaseOrderService.approvePurchaseOrderInfo(purchaseOrder, token,false));
    }

    /**
     * 通过申请表主键查询现有数据
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPurchaseOrderByApprovalId/{approvalId}")
    public ResponseAPI queryPurchaseOrderByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        return ResponseAPI.successData(managerPurchaseOrderService.queryPurchaseOrderByApprovalId(approvalId));
    }

    /**
     * 通过申请表主键查询历史
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPurchaseOrderHistoryByApprovalId/{approvalId}")
    public ResponseAPI queryPurchaseOrderHistoryByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        return ResponseAPI.successData(managerPurchaseOrderService.queryPurchaseOrderHistoryByApprovalId(approvalId));
    }

    /**
     * 获取所有综管采购单
     * @param token
     * @return
     */
    @PostMapping("/queryALLPurchaseOrders")
    public ResponseAPI queryALLPurchaseOrders(@RequestHeader("token") String token){
        return ResponseAPI.successData(managerPurchaseOrderService.queryALLPurchaseOrders(token));
    }

    /**
     * 根据采购id和期数查询计划数据
     * @param paymentPlan
     * @return
     */
    @PostMapping("/queryManagerPayPlanByPidAndPlanPeriod")
    public ResponseAPI queryManagerPayPlanByPidAndPlanPeriod(@RequestBody ManagerPayPlan paymentPlan){
        List<ManagerPayPlan> paymentPlans= managerPurchaseOrderService.queryManagerPayPlanByPidAndPlanPeriod(paymentPlan);
        return ResponseAPI.successData(paymentPlans);
    }

    /**
     * 删除采购单
     * @param purchaseOrdeId
     * @return
     */
    @PostMapping("/deletePurchaseOrderById/{purchaseOrdeId}")
    public ResponseAPI deletePurchaseOrderById(@PathVariable(value = "purchaseOrdeId") int purchaseOrdeId){
        return ResponseAPI.successData(managerPurchaseOrderService.deletePurchaseOrderById(purchaseOrdeId));
    }


    @PostMapping("/exportPurchaseOrdes")
    public void exportPurchaseOrdes(HttpServletResponse response,
                                    @RequestBody ManagerPurchaseOrderVO entit,
                                    @RequestHeader("token") String token) throws Exception{
        managerPurchaseOrderService.exportPurchaseOrdes(response,entit,token);
    }

    /**
     * 分页查询采购待办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurByPage( @PathVariable(value = "pageNum") int pageNum,
                                               @PathVariable(value = "pageSize") int pageSize,
                                               @RequestBody ManagerPurchaseOrderVO entity) {
        IPage<ManagerPurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPurchaseOrderVO> result = managerPurchaseOrderService.queryApprovalPurByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询采购已办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurDoneByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurDoneByPage( @PathVariable(value = "pageNum") int pageNum,
                                                   @PathVariable(value = "pageSize") int pageSize,
                                                   @RequestBody ManagerPurchaseOrderVO entity) {
        IPage<ManagerPurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPurchaseOrderVO> result = managerPurchaseOrderService.queryApprovalPurDoneByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询采购申请记录信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalPurRecordByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalPurRecordByPage( @PathVariable(value = "pageNum") int pageNum,
                                                     @PathVariable(value = "pageSize") int pageSize,
                                                     @RequestBody ManagerPurchaseOrderVO entity,
                                                     @RequestHeader("token") String token) {
        IPage<ManagerPurchaseOrderVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ManagerPurchaseOrderVO> result = managerPurchaseOrderService.queryApprovalPurRecordByPage(page ,entity,token);
        return ResponseAPI.successData(result);
    }


}
